package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfLoanApplyBaseInfoService.java
* Author:   you.yichao
* Date:     2019-10-11
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-10-11          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.database.mybatisplus.PageUtil;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.logger.LogAnnotation;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.common.enums.trade.status.BusinessReviewStatusEnum;
import com.mdb.open.common.enums.trade.status.LoanApplyAmlStatusEnum;
import com.mdb.open.common.enums.trade.status.LoanApplyOrderStatusEnum;
import com.mdb.open.common.enums.trade.status.RiskReviewStatusEnum;
import com.mdb.open.common.enums.trade.type.ProcessLogTypeEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.constant.trade.Constants;
import com.mdb.open.trade.domain.bo.trade.LoanApplyQueryBo;
import com.mdb.open.trade.domain.bo.trade.UpdateLoanApplyBo;
import com.mdb.open.trade.domain.po.trade.LoanApplyBaseInfo;
import com.mdb.open.trade.mapper.trade.TfLoanApplyBaseInfoMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *  贸易融资申请基本资料表 服务类
 *
 * @author you.yichao
 * @date 2019-10-11
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfLoanApplyBaseInfoService extends SuperService<TfLoanApplyBaseInfoMapper, LoanApplyBaseInfo> {

    public ResultEx<LoanApplyBaseInfo> getApplyBaseInfo(LoanApplyQueryBo bo) {
        LambdaQueryWrapper<LoanApplyBaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bo.getBusinessNo())) {
            queryWrapper.eq(LoanApplyBaseInfo::getBusinessNo, bo.getBusinessNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getCustomerCode())) {
            queryWrapper.eq(LoanApplyBaseInfo::getCustomerCode, bo.getCustomerCode());
        }
        if (ObjectUtil.isNotEmpty(bo.getApplyOrderNo())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyOrderNo, bo.getApplyOrderNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getTradeFileNo())) {
            queryWrapper.eq(LoanApplyBaseInfo::getTradeFileNo, bo.getTradeFileNo());
        }
        queryWrapper.eq(LoanApplyBaseInfo::isDeleted, Boolean.FALSE);

        return ResultEx.ok(this.baseMapper.selectOne(queryWrapper));
    }


    public IPage<LoanApplyBaseInfo> pageQuery(LoanApplyQueryBo bo) {

        LambdaQueryWrapper<LoanApplyBaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bo.getBusinessNo())) {
            queryWrapper.eq(LoanApplyBaseInfo::getBusinessNo, bo.getBusinessNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getCustomerCode())) {
            queryWrapper.eq(LoanApplyBaseInfo::getCustomerCode, bo.getCustomerCode());
        }
        if (ObjectUtil.isNotEmpty(bo.getApplyOrderNo())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyOrderNo, bo.getApplyOrderNo());
        }
        if (ObjectUtil.isNotEmpty(bo.getMerchantName())) {
            queryWrapper.like(LoanApplyBaseInfo::getMerchantName, bo.getMerchantName());
        }
        if (ObjectUtil.isNotEmpty(bo.getStartDate()) && ObjectUtil.isNotEmpty(bo.getEndDate())) {
            queryWrapper.between(LoanApplyBaseInfo::getCreateTime, bo.getStartDate(), bo.getEndDate());
        }

        IPage<LoanApplyBaseInfo> pageList = this.baseMapper.selectPage(PageUtil.initPage(bo), queryWrapper);

        return pageList;
    }

    /**
     * 更新融资申请单状态
     *   业务审核状态、风控审核状态，默认值均为：未审批
     */
    @LogAnnotation
    @CacheEvict(value = Constants.LOAN_APPLY_DETAIL, key = "#p0.applyOrderNo")
    public ResultEx updateLoanApplyStatus(UpdateLoanApplyBo bo) {
        LoanApplyBaseInfo updateApplyBaseInfo = new LoanApplyBaseInfo();
        LambdaUpdateWrapper<LoanApplyBaseInfo> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(LoanApplyBaseInfo::getApplyOrderNo, bo.getApplyOrderNo());

        if (ProcessLogTypeEnum.APPLY_COMMIT.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.NOT_INITIATED.getValue());
            updateApplyBaseInfo.setApplyStatus(LoanApplyOrderStatusEnum.UNDER_REVIEW);
        }

        if (ProcessLogTypeEnum.CLAIM_APPLY_REJECT.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.and(i -> i.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.NOT_INITIATED.getValue())
                    .or().eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue())
                    .or().eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.DEPOSIT_IN_ADVANCE.getValue()));

            updateApplyBaseInfo.setApplyStatus(LoanApplyOrderStatusEnum.DEPOSIT_IN_ADVANCE);
            updateApplyBaseInfo.setApprovalStatus(BusinessReviewStatusEnum.CLAIM_REJECT);
            updateApplyBaseInfo.setRiskStatus(RiskReviewStatusEnum.CLAIM_REJECT);
            updateApplyBaseInfo.setBizRejectReason(bo.getRejectReason());
        }

        //业务审批
        if (ProcessLogTypeEnum.BIZ_APPROVE_SUCCESS.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.NOT_HIT.getValue());
            queryWrapper.and(i -> i.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.UN_REVIEW.getValue())
                    .or().eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.STAFF_REJECT.getValue()));

            updateApplyBaseInfo.setApprovalStatus(BusinessReviewStatusEnum.SUCCESS);
            updateApplyBaseInfo.setRiskStatus(RiskReviewStatusEnum.UN_REVIEW);
        }

        if (ProcessLogTypeEnum.BIZ_APPROVE_REJECT.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.and(i -> i.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.UN_REVIEW.getValue())
                    .or().eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.STAFF_REJECT.getValue()));
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.NOT_HIT.getValue());

            updateApplyBaseInfo.setApprovalStatus(BusinessReviewStatusEnum.STAFF_REJECT);
            updateApplyBaseInfo.setApplyStatus(LoanApplyOrderStatusEnum.REJECT);
            updateApplyBaseInfo.setBizRejectReason(bo.getRejectReason());
        }

        //风控审批（业务审批一定通过）
        if (ProcessLogTypeEnum.RISK_APPROVE_SUCCESS.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.SUCCESS.getValue());
            queryWrapper.and(i -> i.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.UN_REVIEW.getValue())
                    .or().eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.STAFF_REJECT.getValue()));
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.NOT_HIT.getValue());

            //确保该阶段的业务状态一定是：通过
            updateApplyBaseInfo.setApprovalStatus(BusinessReviewStatusEnum.SUCCESS);
            updateApplyBaseInfo.setRiskStatus(RiskReviewStatusEnum.STAFF_APPROVE);
        }

        if (ProcessLogTypeEnum.RISK_APPROVE_REJECT.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.SUCCESS.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.NOT_HIT.getValue());

            //业务审核状态初始化为未审核（业务人员需要重审, 业务审核状态：未审核）
            updateApplyBaseInfo.setApprovalStatus(BusinessReviewStatusEnum.UN_REVIEW);
            updateApplyBaseInfo.setRiskStatus(RiskReviewStatusEnum.STAFF_REJECT);
            updateApplyBaseInfo.setRiskRejectReason(bo.getRejectReason());
        }

        if (ProcessLogTypeEnum.RISK_SYS_APPROVE_SUCCESS.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.SUCCESS.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.STAFF_APPROVE.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.NOT_HIT.getValue());

            updateApplyBaseInfo.setApplyStatus(LoanApplyOrderStatusEnum.SUCCESS);
            updateApplyBaseInfo.setRiskStatus(RiskReviewStatusEnum.SUCCESS);
        }

        if (ProcessLogTypeEnum.RISK_SYS_APPROVE_REJECT.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.SUCCESS.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.STAFF_APPROVE.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.NOT_HIT.getValue());

            updateApplyBaseInfo.setApplyStatus(LoanApplyOrderStatusEnum.REJECT);
            updateApplyBaseInfo.setRiskStatus(RiskReviewStatusEnum.STRATEGY_REJECT);
            updateApplyBaseInfo.setRiskRejectReason(bo.getRejectReason());
        }

        if (ProcessLogTypeEnum.AML_PROCESSING.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.UN_REVIEW.getValue());

            updateApplyBaseInfo.setAmlStatus(LoanApplyAmlStatusEnum.SUSPECT_HIT);
        }

        if (ProcessLogTypeEnum.AML_APPROVE_SUCCESS.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.UN_REVIEW.getValue());

            updateApplyBaseInfo.setAmlStatus(LoanApplyAmlStatusEnum.NOT_HIT);
        }

        if (ProcessLogTypeEnum.AML_APPROVE_REJECT.getValue().equals(bo.getStatus().getValue())) {
            queryWrapper.eq(LoanApplyBaseInfo::getApplyStatus, LoanApplyOrderStatusEnum.UNDER_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getApprovalStatus, BusinessReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getRiskStatus, RiskReviewStatusEnum.UN_REVIEW.getValue());
            queryWrapper.eq(LoanApplyBaseInfo::getAmlStatus, LoanApplyAmlStatusEnum.UN_REVIEW.getValue());

            updateApplyBaseInfo.setApplyStatus(LoanApplyOrderStatusEnum.REJECT);
            updateApplyBaseInfo.setAmlStatus(LoanApplyAmlStatusEnum.MANUAL_HIT);
            updateApplyBaseInfo.setAmlRejectReason(bo.getRejectReason());
        }

        if (this.baseMapper.update(updateApplyBaseInfo, queryWrapper) != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

}
